package com.lin.controller;


import com.lin.pojo.PageVo;
import com.lin.pojo.SysMenus;
import com.lin.pojo.SysRoles;
import com.lin.pojo.SysUsers;
import com.lin.service.SysRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class SysRolesController {

     @Autowired
    SysRolesService sysRolesService;

    //显示一级菜单
    @RequestMapping("/rolesbymenus.action")
    public SysRoles rolesbyMenus(Integer id,HttpServletRequest request) {
        //Session取值
        HttpSession session = request.getSession();
        //转成SysUsers类
        SysUsers sysUsers = (SysUsers) session.getAttribute("sysUsers");

        SysRoles roles=sysRolesService.rolesbyMenus(sysUsers,id);
        return roles;
    }

    //循环查找所以子菜单
    @RequestMapping("/querybymenus.action")
    public List<SysMenus> querybymenus(Integer id,HttpServletRequest request) {
        //Session取值
        HttpSession session = request.getSession();
        //转成SysUsers类
        SysUsers sysUsers = (SysUsers) session.getAttribute("sysUsers");

        return sysRolesService.querybymenus(sysUsers,id);
    }
    @PostMapping("/sysroles.action")
    public boolean addSysroles(SysRoles sysRoles){
        return sysRolesService.save(sysRoles);
    }
    @DeleteMapping("/sysroles.action/{id}")
    public Map delSysroles(@PathVariable int id){
        boolean up= sysRolesService.removeById(id);
        Map map=new HashMap();
        map.put("msg","删除失败");
        if (up) {
            map.put("msg","删除成功");
        }
        return map;
    }
    @PutMapping("/sysroles.action")
    public Map upddateSysroles(@RequestBody SysRoles sysRoles){
        boolean up= sysRolesService.updateById(sysRoles);
        Map map=new HashMap();
        map.put("msg","修改失败");
        if (up) {
            map.put("msg","修改成功");
            map.put("code","1");
        }
        return map;
    }
    @GetMapping("/sysroles.action/{id}")
    public SysRoles querySysrolesbyid(@PathVariable int id){
        return sysRolesService.getById(id);
    }
    @GetMapping("/sysroles.action")
    public PageVo<SysRoles> queryPersonbycond(SysRoles sysRoles,
                                              @RequestParam(value = "pageno",defaultValue = "1") Integer pageno,
                                              @RequestParam(value = "pagesize",defaultValue = "5")Integer pagesize){
        return sysRolesService.querySysrolesbycond(sysRoles,pageno,pagesize);
    }
}